package com.zzq.my.jetpacks.jectpackapp.ui.fragment

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.orhanobut.logger.Logger
import com.zzq.my.jetpacks.jectpackapp.R

/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2022/11/23-13:53
 * 邮箱：zzq@eeepay.cn
 * 备注: MyBottomSheetDialog().show(supportFragmentManager, "MyBottomSheetDialog")
 *
 * BottomSheetDialogFragment只会显示实际高度，即布局有效高度，即使根布局高度match_parent也不行。
 */
class MyBottomSheetDialog:BottomSheetDialogFragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.BottomSheetDialog)
    }
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_goods_sku_layout, null)
        dialog.setContentView(view)
        initView(view)
        return dialog
    }

    override fun onStart() {
        super.onStart()
        //拿到系统的 bottom_sheet(design_bottom_sheet找不到,可能要写成com.google.android.material.R.id.design_bottom_sheet)
        val view: FrameLayout = dialog?.findViewById(R.id.design_bottom_sheet)!!
        //设置view高度
        view.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
        //获取behavior
        val behavior = BottomSheetBehavior.from(view)
        //设置弹出高度(这个弹窗一开始并不是完全展开的，但是可以继续拉出来。)
        behavior.peekHeight = 0
        //设置展开状态
        behavior.state = BottomSheetBehavior.STATE_EXPANDED
        //监听展开收起
        /**
         * STATE_COLLAPSED： 折叠状态
        STATE_EXPANDED： 展开状态
        STATE_DRAGGING ： 过渡状态
        STATE_SETTLING：  视图从脱离手指自由滑动到最终停下的这一小段时间
        STATE_HIDDEN ： 默认无此状态（可通过app:behavior_hideable 启用此状态），启用后用户将能通过向下滑动完全隐藏 bottom sheet

         */
        behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {

            override fun onStateChanged(bottomSheet: View, newState: Int) {
                when(newState){
                    BottomSheetBehavior.STATE_EXPANDED->{
                        Logger.d("展开")
                        //如果是展开状态，则关闭，反之亦然
//                        behavior.state = BottomSheetBehavior.STATE_COLLAPSED
                    }//展开
                    BottomSheetBehavior.STATE_COLLAPSED->{
                        Logger.d("收起")
                        //设置展开状态
//                        behavior.state = BottomSheetBehavior.STATE_EXPANDED
                    }//收起
                    BottomSheetBehavior.STATE_DRAGGING->{
                        Logger.d("DRAGGING")
                    }
                    BottomSheetBehavior.STATE_SETTLING->{
                        Logger.d("SETTLING")
                    }
                    BottomSheetBehavior.STATE_HIDDEN->{
                        Logger.d("隐藏")
                    }//隐藏
                }
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {

            }

        })
    }

    private fun initView(rootView: View) {
        //do something
        rootView.findViewById<TextView>(R.id.tv_orders_sku).setOnClickListener { dismiss() }

    }

}